<?php

/*
 * Copyleft 2015 The TFT
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 */

class ModelePDO {

    //Attributs utiles pour la connexion
    protected static $serveur = MySqlConfig::SERVEUR;
    protected static $base = MySqlConfig::BASE;
    protected static $utilisateur = MySqlConfig::UTILISATEUR;
    protected static $passe = MySqlConfig::MOT_DE_PASSE;
    //Attributs utiles pour la manipulation PDO de la BD
    /**
     *
     * @var PDO
     */
    protected static $pdoCnxBase = null;

    /**
     *
     * @var PDOStatement
     */
    protected static $pdoStResults = null;
    protected static $requete = "";
    protected static $resultat = null;

    /**
     * Permet de se connecter à la base de données
     * Retourne true si la connexion est établie, false sinon
     */
    protected static function seConnecter() {

        //Doc officielle connexion/déconnexion : http://www.php.net/manual/fr/pdo.connections.php
        //voir remarque sur les connexions PERSISTANTES : une seule connexion et plus rapide ensuite...
        // mais dans certains cas... Attention aux nombres de connexions simultanées


        if (!isset(self::$pdoCnxBase)) { //Il n'y a pas encore eu de connexion
            try {
                self::$pdoCnxBase = new PDO('mysql:host=' . self::$serveur . ';dbname=' . self::$base, self::$utilisateur, self::$passe);
                self::$pdoCnxBase->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                self::$pdoCnxBase->query("SET CHARACTER SET utf8");
            } catch (Exception $e) {
                echo $e->getMessage();
            }
        }
    }

    protected static function seDeconnecter() {
        self::$pdoCnxBase = null;

        //si on n'appelle pas la méthode, la déconnexion a lieu en fin de script
    }

    public static function getLesTuples($table) {
        self::seConnecter();

        self::$requete = "SELECT * FROM " . $table;
        self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
        self::$pdoStResults->execute();
        self::$resultat = self::$pdoStResults->fetchAll(PDO::FETCH_OBJ);

        self::$pdoStResults->closeCursor();

        return self::$resultat;
    }

    public static function getPremierTupleByChamp($table, $nomChamp, $valeurChamp) {
        self::seConnecter();

        self::$requete = "SELECT * FROM " . $table . " WHERE " . $nomChamp . " = :valeurChamp";
        self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
        self::$pdoStResults->bindParam(':valeurChamp', $valeurChamp);
        self::$pdoStResults->execute();
        self::$resultat = self::$pdoStResults->fetch(PDO::FETCH_OBJ);
        self::$pdoStResults->closeCursor();

        return self::$resultat; //un seul tuple retourné : utilization de fetch()
    }

    public static function getLesTuplesByChamp($table, $nomChamp, $valeurChamp) {

        self::seConnecter();

        self::$requete = "SELECT * FROM " . $table . " WHERE " . $nomChamp . " = :valeurChamp";
        self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
        self::$pdoStResults->bindParam(':valeurChamp', $valeurChamp);
        self::$pdoStResults->execute();
        self::$resultat = self::$pdoStResults->fetchAll(PDO::FETCH_OBJ);

        self::$pdoStResults->closeCursor();

        return self::$resultat;
    }

    public static function supprimerTupleByChamp($table, $nomChamp, $valeurChamp) {

        self::seConnecter();
        $requete = "DELETE FROM " . $table . " WHERE " . $nomChamp . " = :valeurChamp;";

        self::$pdoStResults = self::$pdoCnxBase->prepare($requete);
        self::$pdoStResults->bindParam(':valeurChamp', $valeurChamp);

        self::$pdoStResults->execute();
    }

    public static function select($champs, $tables, $conditions = null) {
        self::seConnecter();

        self::$requete = "SELECT " . $champs . " FROM " . $tables;

        if ($conditions != null)
            self::$requete .= " WHERE " . $conditions . ";";

        self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);

        self::$pdoStResults->execute();
        self::$resultat = self::$pdoStResults->fetch(PDO::FETCH_OBJ);

        self::$pdoStResults->closeCursor();

        return self::$resultat;
    }

    public static function getRows($champs, $tables, $conditions = null) {
        self::seConnecter();

        self::$requete = "SELECT " . $champs . " FROM " . $tables;
        if ($conditions != null) {
            self::$requete.= " WHERE " . $conditions;
        }

        self::$pdoStResults = self::$pdoCnxBase->prepare(self::$requete);
        self::$pdoStResults->execute();
        self::$pdoStResults->fetchAll(PDO::FETCH_OBJ);
        return self::$pdoStResults->rowCount();
    }

    public static function getDateJour() {
        $jour = date('d');
        $mois = date('m');
        $annee = date('Y');
        $heure = date('H') + 2;
        $minute = date('i');
        $sec = date('s');
        return $dateDuJour = $jour . '/' . $mois . '/' . $annee . '  ' . $heure . ':' . $minute . ':' . $sec;
    }

}

?>
